layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    var params = new URLSearchParams(window.location.search);
    var deptId = params.get('deptId');
    var deptName = params.get('deptname');
    console.log(deptId,deptName);

    /**
     * 商品表管理
     */
    var sysPositions = {
        tableId: "sysPositionsTable"
    };

    /**
     * 初始化表格的列
     */
    sysPositions.initColumn = function () {

        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'positionsName', sort: true, title: '岗位名称'},
            {field: 'positionsZz', sort: true, title: '岗位职责'},
            {field: 'createTime', sort: true, title: '创建时间'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    sysPositions.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(sysPositions.tableId, {
            where: queryData, page: {curr: 1},
            done:function () {
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + sysPositions.tableId,
        url: Feng.ctxPath + '/sysPositions/list?deptId='+deptId,
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: sysPositions.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        sysPositions.search();
    });

    // 批量分配物料编码
    $('#btnAssign').click(function () {
        sysPositions.btnAssign();
    });

    //路径
    function getpage(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(r[2]);
        } else {
            return null;
        }
    };



    // 批量分配物料编码
    sysPositions.btnAssign = function () {
        var checkStatus = table.checkStatus('sysPositionsTable');
        var data2 = checkStatus.data;
        console.log(data2)
        if (data2.length>0) {
            layer.confirm('是否设置选中岗位?', function(index){
                layer.close(index);

                for (var i = 0; i < data2.length; i++) {
                    console.log(data2[i].id)
                    $.ajax({
                        url: Feng.ctxPath + "/sysDeptPositions/addItem",
                        cache: false,
                        type: "POST",
                        dataType: "json",
                        data: {
                            positionsId: data2[i].id,
                            positionsName: data2[i].positionsName,
                            deptId: deptId,
                            deptName: deptName
                        },
                        beforeSend: function () {
                            //弹出加载中
                            index = layer.load(1, {
                                shade: [0.3, '#000'] //0.1透明度的白色背景
                            });
                        },
                        success: function (data) {
                            layer.close(index);
                            if (data.success) {
                                Feng.success("添加成功!");
                                var Indexs = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(Indexs);
                            } else {
                                Feng.error("添加失败！" + data.message)
                            }
                        },
                        error: function (data) {
                            layer.close(index);
                            Feng.error("添加失败！" + data.responseJSON.message)
                        }
                    })
                }
            });
        } else {
            layer.alert('请至少选中一个岗位');
        }
    };

});